#pragma once
#include "CVMiscellaneous.h"
#include "cvplot.h"
class Plotter : public IPlotter
{
public:
	Plotter();
	virtual ~Plotter();
	virtual cv::Mat plotUchar(const std::string figure_name, const unsigned char* p, int count, int step = 1, int R = -1, int G = -1, int B = -1, const std::string label = "") override;
	virtual cv::Mat plotInt(const std::string figure_name, const int* p, int count, int step = 1, int R = -1, int G = -1, int B = -1, const std::string label = "") override;
	virtual cv::Mat plotFloat(const std::string figure_name, const float* p, int count, int step = 1, int R = -1, int G = -1, int B = -1, const std::string label = "") override;
	virtual void clear(const std::string figure_name) override;

private:
	// plot a new curve, if a figure of the specified figure name already exists,
	// the curve will be plot on that figure; if not, a new figure will be created.
	// static method
	// handle different data types; static mathods;
	template<typename T>
	void plotFigure(const string figure_name, const T* p, int count, int step = 1,
		int R = -1, int G = -1, int B = -1, const string& label = "")
	{
		if (step <= 0)
			step = 1;

		float  *data_copy = new float[count * step];

		float   *dst = data_copy;
		const T *src = p;

		for (int i = 0; i < count * step; i++)
		{
			*dst = (float)(*src);
			dst++;
			src++;
		}

		pm.PlotFigure(figure_name, data_copy, count, step, R, G, B, label);
		delete[] data_copy;
	}
	template<typename T>
	cv::Mat plotImage(const string figure_name, const T* p, int count, int step = 1,
		int R = -1, int G = -1, int B = -1, const string& label = "")
	{
		if (step <= 0)
			step = 1;

		float  *data_copy = new float[count * step];

		float   *dst = data_copy;
		const T *src = p;

		for (int i = 0; i < count * step; i++)
		{
			*dst = (float)(*src);
			dst++;
			src++;
		}

		cv::Mat res = pm.PlotImage(figure_name, data_copy, count, step, R, G, B, label);
		delete[] data_copy;
		return res;
	}

	void Innerclear(const string figure_name);

private:
	CvPlot::PlotManager pm;

};

